/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.sober.transaction;

import com.jspx.sober.SoberSupport;
import com.jspx.sober.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2008-8-18
 * Time: 14:03:08
 */
public abstract class AbstractTransaction implements Transaction
{
    protected static final Logger log = LoggerFactory.getLogger(AbstractTransaction.class);
    //事务管理器
    final static TransactionManager transactionManager = TransactionManager.getInstance();
    protected Connection connection = null;
    boolean wasRolledBack = false;
    boolean wasCommitted = false;
    int isActive = 0;
    protected int timeout = 30;
    protected boolean supportsSavePoints = false;
    SoberSupport soberSupport = null;
    private long createTimeMillis = System.currentTimeMillis();
    public long getCreateTimeMillis()
    {
        return createTimeMillis;
    }

    public boolean isSupportsSavePoints() {
        return supportsSavePoints;
    }

    public void setSupportsSavePoints(boolean supportsSavePoints) {
        this.supportsSavePoints = supportsSavePoints;
    }

    public int getTid()
    {
        return connection.hashCode();
    }

    public SoberSupport getSoberSupport() {
        return soberSupport;
    }

    void setSoberSupport(SoberSupport soberSupport) {
        this.soberSupport = soberSupport;
    }

    public Connection getConnection()
    {
        return connection;
    }

    public void setConnection(Connection connection)
    {
        this.connection = connection;
    }

    public boolean wasRolledBack()
    {
        return wasRolledBack;
    }

    public boolean wasCommitted()
    {
        return wasCommitted;
    }

    public boolean isActive()
    {
        return connection != null && isActive != 0;
    }

    public void setTimeout(int timeout)
    {
        this.timeout = timeout;
    }

    public int getTimeout()
    {
        return this.timeout;
    }

    public boolean isClosed()
    {
        try {
            return  isActive == 0 && (wasRolledBack || wasCommitted) || soberSupport == null || connection.isClosed();
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

}